<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/15 0015
 * Time: 11:40
 */

namespace Admin\Controller;


class ProjectController extends AuthController{

    public function index(){
        $post = I('post.');
        //dump($post);
        $keywords = $post['keywords'];
        $map['pro_name'] = array('like' , "%$keywords%");
        $pro = M('project');

        if(!empty($post["cid"])){
            $count = $pro->where('project_category_id ='.$post['cid'])->count();
        }else if(!empty($post['keywords'])){
            $count = $pro->where($map)->count();
        }else{
            $count = $pro->count();
        }

        $Page = new \Think\Page($count,5);
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','末页');
        $Page->setConfig('first','首页');
        $show = $Page->show();
        if(!empty($post["cid"])){
            $pros = $pro->order('create_time')->where('project_category_id ='.$post['cid'])->limit($Page->firstRow.','.$Page->listRows)->select();
        }else if(!empty($post['keywords'])){
            $pros = $pro->order('create_time')->where($map)->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $pros = $pro->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        }


        //dump($pros);
        $procate = M('project_category')->select();
        $this->assign('procate',$procate);
        $this->assign('page',$show);
        $this->assign('pros',$pros);
        $this->display('project');
    }

    public function add($id){
        $pro = D('project');
        $pros = $pro->find($id);
        //dump($pros);
        $this->assign('pros',$pros);
        $this->display('add');
    }

    public function edit($id){
        $pro = D('project');
        $pros = $pro->find($id);
        //dump($pros);
        if(IS_POST){
            $post = I('post.');
            //dump($post);exit;
            if($pro->create()){
                if($pro->save($post)){
                    $this->success('编辑成功',U('project/index'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($pro->getError());
            }
        }
        $this->assign('pros',$pros);
        $this->display('editproject');
    }

    public function del($id){
        $pro = M('project');
        $pros = $pro->delete($id);
        if($pros){
            $this->success('删除成功！' , U('project/index'));
        }else{
            $this->error('删除失败');
        }
    }

    public function project_category(){
        $procate = M('project_category')->select();
        //dump($procate);
        $this->assign('procate', $procate);
        $this->display('project_category');
    }

    public function cateadd(){
        $procate = M('project_category');
        if(IS_POST){
            //dump(I('post.'));exit;
            $post = I('post.');
            if(!empty($post['project_category_name'])  && !empty($post['sort'])){
                if($procate->create()){
                    if($procate->add()){
                        $this->success('添加成功', U('project/project_category'));
                    }else{
                        $this->error('添加失败');
                    }
                }else{
                    $this->error($procate->getError());
                }
            }else{
                $this->error('请输入内容！！！！！');
            }
        }
    }

    public function cateedit($id){
        $procate = M('project_category');
        $procates = $procate->find($id);
        if(IS_POST){
            $post = I('post.');
            //dump($post);exit;
            if(!empty($post['project_category_name'])  && !empty($post['sort'])){
                if($procate->create()){
                    if($procate->save($post)){
                        $this->success('编辑成功',U('project/project_category'));
                    }else{
                        $this->error('编辑失败');
                    }
                }else{
                    $this->error($procate->getError());
                }
            }else{
                $this->error('请输入内容！！！！！');
            }
        }
        $this->assign('procates',$procates);
        $this->display('cateedit');
    }


    public function catedel($id){
        $procate = M('project_category');
        $procates = $procate->delete($id);
        if($procates){
            $this->success('删除成功！' , U('project/project_category'));
        }else{
            $this->error('删除失败');
        }
    }

}